/* 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.shanekjohnson.cmis.client.atompub.model
{
	import mx.collections.ArrayCollection;
	
	public class Folder
	{
		private namespace app = "http://www.w3.org/2007/app";
		private namespace atom = "http://www.w3.org/2005/Atom";
		private namespace cmis = "http://docs.oasis-open.org/ns/cmis/core/200908/";
		private namespace cmisra = "http://docs.oasis-open.org/ns/cmis/restatom/200908/";
		
		use namespace app;
        use namespace atom;
        use namespace cmis;
        use namespace cmisra;
		
		public var name:String;
		public var childrenURL:String;
		
		public var opened:Boolean = false;
		
		[Bindable] public var children:ArrayCollection;
		[Bindable] public var documents:ArrayCollection;
		
		public function Folder()
		{
			children = new ArrayCollection();
			documents = new ArrayCollection();
		}
		
		public static function getFolder(f:XML):Folder 
		{
			var folder:Folder = new Folder();
			
			folder.name = f.title;
			
			for each(var link:XML in f.link) 
			{
				if(link.@rel != undefined)  // links without "rel" are allowed for feed readers:
				{
					var pattern:RegExp = / /gi;  
					
					var type:String = link.@type;
					
					var typeValue:String = type.replace(pattern, '');
					
					if(link.@rel == 'down' && typeValue == 'application/atom+xml;type=feed') 
					{
						folder.childrenURL = link.@href;
					}
					
				}
				
			} 
			
			return folder;
		}

	}
	
}